﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;



namespace NyMPHTestWrappers
{
    public partial class NyMPHDataTestWrapper : Form
    {

        private NyMPHTestWrappers.Properties.Settings MySettings = new NyMPHTestWrappers.Properties.Settings();
        
        private SqlCeConnection _conn;

        
        public NyMPHDataTestWrapper()
        {
            InitializeComponent();


            txtDB.Text = MySettings.LastDBPath;

            _conn = new SqlCeConnection(string.Format(@"Data Source={0};Persist Security Info=False;", MySettings.LastDBPath));
        }



        private void btnClear_Click(object sender, EventArgs e)
        {
            txtSQL.Clear();
        }

        private void btnRunAction_Click(object sender, EventArgs e)
        {
            using (SqlCeCommand _cmd = new SqlCeCommand("", _conn))
            {
                try
                {
                    _cmd.CommandText = txtSQL.Text;

                    _cmd.Connection.Open();
                    txtOutput.Text = _cmd.ExecuteNonQuery().ToString() + " records affected";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
                finally
                {
                    _cmd.Connection.Close();
                    _cmd.Dispose();
                }
            }
        }

        private void btnRunQuery_Click(object sender, EventArgs e)
        {
            using (SqlCeCommand _cmd = new SqlCeCommand("", _conn))
            {

                try
                {
                    _cmd.CommandText = txtSQL.Text;

                    SqlCeDataAdapter _da = new SqlCeDataAdapter(_cmd);

                    DataTable _dt = new DataTable();

                    _da.Fill(_dt);

                    dgvMain.DataSource = _dt;



                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);

                }
                finally
                {
                    _cmd.Dispose();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (String.IsNullOrEmpty(txtDB.Text))
            {
                txtDB.Text = @"c:\temp";
            }
            ofd.InitialDirectory = System.IO.Path.GetDirectoryName(txtDB.Text);

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtDB.Text = ofd.FileName;
                MySettings.LastDBPath = ofd.FileName;
                _conn.ConnectionString = string.Format(@"Data Source={0};Persist Security Info=False;", MySettings.LastDBPath);

            }


        }

        private void NyMPHDataTestWrapper_FormClosing(object sender, FormClosingEventArgs e)
        {
            MySettings.Save();

        }
    }
}
